package com.hellogood.yaohuo.client.bussiness;

import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.data.Constants;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabDeselectedEvent;
import com.smartgwt.client.widgets.tab.events.TabDeselectedHandler;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public abstract class AbstractTab extends Tab {
	protected TabSet tabSet = null;
	protected Button newBtn = null;
	protected Button modifyBtn = null;
	protected Button saveBtn = null;
	protected Button cancelBtn = null;
	protected Button deleteBtn = null;
	protected Button bankBtn = null;
	protected Button printBtn = null;
	protected SearchTab searchTab = null;
	protected FormTab formTab = null;
	protected ListGrid searchListGrid = null;

	private boolean isNewBtnCreated = false;

	public static String status = "";

	public AbstractTab() {
		initSearch();
		initForm();
		initView();
		initPrintBtn();
	}

	protected void initPrintBtn() {

	}

	public abstract void initSearch();

	public abstract void initForm();

	public abstract void showFormDetail();

	private void initView() {
		searchTab.setTitle(Constants.SEARCH);
		formTab.setTitle(Constants.FORM);

		tabSet = new TabSet();
		tabSet.addTab(searchTab);
		tabSet.addTab(formTab);

		searchTab.addTabSelectedHandler(new TabSelectedHandler() {

			@Override
			public void onTabSelected(TabSelectedEvent event) {
				showRzSuccessful();
				printBtn.setTitle(Constants.BUTTON_PRINT);
			}
		});

		formTab.addTabDeselectedHandler(new TabDeselectedHandler() {

			@Override
			public void onTabDeselected(TabDeselectedEvent event) {
				if (!cancelBtn.isDisabled()) {
					event.cancel();
//					tabSet.selectTab(formTab);
				}
			}
		});

		formTab.addTabSelectedHandler(new TabSelectedHandler() {

			@Override
			public void onTabSelected(TabSelectedEvent event) {
				printBtn.setTitle(Constants.BUTTON_PRINT_MODE);
				if (formTab.getFormController().getTitleLabel() != null) {
					formTab.getFormController().getTitleLabel().setVisible(true);
				}
				if (formTab.getFormController().getForm() != null) {
					formTab.getFormController().getForm().setVisible(true);
				}
				if (formTab.getFormController().getListGrid() != null) {
					formTab.getFormController().getListGrid().setVisible(true);
				}
				if (formTab.getFormController().getPrintView() != null) {
					formTab.getFormController().getPrintView().setVisible(false);
				}
				if (newBtn.isDisabled()) {
					formTab.getFormController().initForm();
					showCreateBill();
				} else if (!newBtn.isDisabled()) {
					formTab.getFormController().getOrgItem().focusInItem();
				}
				// if (formTab.getFormController().getStatus() ==
				// Constants.FORM_STATUS_BANKED) {
				showRzSuccessful();
				if (formTab.getFormController().getStatus() == Constants.FORM_STATUS_SAVED) {
					showSaveSuccessful();
				}
				if (!isNewBtnCreated) {
					showFormDetail();
				}
				isNewBtnCreated = false;
			}
		});

		searchListGrid = searchTab.getSearchController().getListGrid();

		newBtn = new Button(Constants.BUTTON_NEW);
		newBtn.addClickHandler(new NewBtnClickHandler());
		modifyBtn = new Button(Constants.BUTTON_MODIFY);
		modifyBtn.setDisabled(true);
		modifyBtn.addClickHandler(new ModifyBtnClickHandler());
		saveBtn = new Button(Constants.BUTTON_SAVE);
		saveBtn.setDisabled(true);
		saveBtn.addClickHandler(new SaveBtnClickHandler());
		cancelBtn = new Button(Constants.BUTTON_CANCEL);
		cancelBtn.setDisabled(true);
		cancelBtn.addClickHandler(new CancelBtnClickHandler());
		deleteBtn = new Button(Constants.BUTTON_DELETE);
		deleteBtn.setDisabled(true);
		deleteBtn.addClickHandler(new DeleteBtnClickHandler());
		bankBtn = new Button(Constants.BUTTON_BANK);
		bankBtn.setDisabled(true);
		bankBtn.addClickHandler(new BankBtnClickHandler());
		printBtn = new Button(Constants.BUTTON_PRINT);

		ToolStrip toolStrip = new ToolStrip();
		toolStrip.setMembers(newBtn, modifyBtn, saveBtn, cancelBtn, deleteBtn, bankBtn, printBtn);
		toolStrip.setMembersMargin(20);
		VLayout layout = new VLayout();
		layout.addMember(toolStrip);
		layout.addMember(tabSet);
		layout.setMembersMargin(10);

		setCanClose(true);
		setPane(layout);
	}

	private class NewBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			isNewBtnCreated = true;
			tabSet.selectTab(formTab);
			formTab.getFormController().getListGrid().setDisabled(false);
			formTab.getFormController().initForm();
			showCreateBill();
		}
	}

	private class ModifyBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			tabSet.selectTab(formTab);
			formTab.getFormController().modifyForm();			
			showModifyBill();
		}
	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			tabSet.selectTab(formTab);
			formTab.getFormController().getListGrid().saveAllEdits();
			formTab.getFormController().getListGrid().setDisabled(false);
			saveForm();
		}
	}

	abstract public void saveForm();

	private class CancelBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmCancel, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						formTab.getFormController().cancelForm();
						showRzSuccessful();
					}
				}
			});
		}
	}

	private class DeleteBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmDelete, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						formTab.getFormController().deleteForm();
					}
				}
			});
		}
	}

	private class BankBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmBank, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						formTab.getFormController().bankForm();
						searchTab.getSearchController().bankForm();
						showRzSuccessful();
					}
				}
			});
		}
	}

	public void showCreateBill() {
		newBtn.setDisabled(true);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(false);
		cancelBtn.setDisabled(false);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

	public void showUnRzBill() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(false);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(false);
		bankBtn.setDisabled(false);
	}

	public void showRzBill() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

	public void showModifyBill() {
		newBtn.setDisabled(true);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(false);
		cancelBtn.setDisabled(false);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

	public void showSaveSuccessful() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(false);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(false);
		bankBtn.setDisabled(false);
	}

	public void showRzSuccessful() {
		newBtn.setDisabled(false);
		modifyBtn.setDisabled(true);
		saveBtn.setDisabled(true);
		cancelBtn.setDisabled(true);
		deleteBtn.setDisabled(true);
		bankBtn.setDisabled(true);
	}

}
